package smecta.client.print.util;

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.TableView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import smecta.client.print.domain.ScpPrintItemConfig;
import smecta.client.print.mapper.ScpPrintItemConfigMapper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DatabaseUtil {

    @Autowired
    private ScpPrintItemConfigMapper itemConfigMapper;


    // ��ѯ����
    public List<RowData> queryItemConfigData(List<String> cols) {
        List<ScpPrintItemConfig> data = itemConfigMapper.selectAll();
        List<RowData> result = new ArrayList<RowData>();
        data.forEach(item -> {
            RowData row = new RowData();
            Map<String, String> map = obj2Map(item);
            for (String id : cols) {
                row.setCellValue(id, new SimpleStringProperty(map.get(id.toUpperCase())));
            }
            result.add(row);
        });
        return result;
    }

    public void saveAllDataByTable(TableView<RowData> dataTableView) {
        itemConfigMapper.delete(null);
        dataTableView.getItems().forEach(item -> {
            ScpPrintItemConfig scpPrintItemConfig = new ScpPrintItemConfig();
            scpPrintItemConfig.setTITLE1(getValue(item.getCellValue("Title1")));
            scpPrintItemConfig.setTITLE2(getValue(item.getCellValue("Title2")));
            scpPrintItemConfig.setTITLE3(getValue(item.getCellValue("Title3")));
            scpPrintItemConfig.setTITLE4(getValue(item.getCellValue("Title4")));
            scpPrintItemConfig.setTITLE5(getValue(item.getCellValue("Title5")));
            scpPrintItemConfig.setTITLE6(getValue(item.getCellValue("Title6")));
            scpPrintItemConfig.setTITLE7(getValue(item.getCellValue("Title7")));
            scpPrintItemConfig.setTITLE8(getValue(item.getCellValue("Title8")));
            scpPrintItemConfig.setTITLE9(getValue(item.getCellValue("Title9")));
            scpPrintItemConfig.setTITLE10(getValue(item.getCellValue("Title10")));
            scpPrintItemConfig.setTITLE11(getValue(item.getCellValue("Title11")));
            scpPrintItemConfig.setTITLE12(getValue(item.getCellValue("Title12")));
            scpPrintItemConfig.setTITLE13(getValue(item.getCellValue("Title13")));
            scpPrintItemConfig.setTITLE14(getValue(item.getCellValue("Title14")));
            scpPrintItemConfig.setTITLE15(getValue(item.getCellValue("Title15")));
            scpPrintItemConfig.setTITLE16(getValue(item.getCellValue("Title16")));
            scpPrintItemConfig.setTITLE17(getValue(item.getCellValue("Title17")));
            itemConfigMapper.insertSelective(scpPrintItemConfig);
        });
    }

    public static Map<String, String> obj2Map(Object obj) {
        Map<String, String> map = new HashMap<>();
        try {
            Class<?> clazz = obj.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true); // 允许访问私有字段
                String fieldName = field.getName();
                Object fieldValue = field.get(obj);
                if (fieldValue instanceof String) {
                    map.put(fieldName, (String) fieldValue);
                } else {
                    // 这里简单忽略非字符串类型的字段，实际应用中可能需要更复杂的处理逻辑
                    System.out.println("字段 " + fieldName + " 的类型不是String，已忽略。");
                }
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        return map;
    }

    private static String getValue(SimpleStringProperty property) {
        if (property != null) {
            return property.get();
        }
        return null;
    }
}
